<?php

namespace app\index\controller;

use think\Db as userBuddyDB;
use think\Log;
use think\Cache;


class UserBuddy extends Common{

    public $datas;

    /**
     * 增加名片夹
     */
    public function addUserBuddy(){
        /**
         * userId              名片编号
         * buddyUserId         好友编号
         */
        $this->datas = $this->params;

        $this->addUserBuddyService();
    }


    private function addUserBuddyService(){

        //判断数据是否存在
        $sql = "SELECT id FROM user_buddy where user_id = ".$this->datas['userId']." and buddy_user_id = ".$this->datas['buddyUserId']." and status = 1";
        $res = userBuddyDB::query($sql);

        if(empty($res)){
            $insertData = [];
            $insertData['user_id'] = $this->datas['userId'];
            $insertData['buddy_user_id'] = $this->datas['buddyUserId'];
            $insertData['create_time'] = date('Y-m-d H:i:s');
            $insertData['update_time'] = date('Y-m-d H:i:s');
            $insertData['status'] = 1;

            userBuddyDB::table('user_buddy')->insert($insertData);
        }


        $this->returnMsg(1, '操作成功！');
    }


    /**
     * 名片夹名片列表
     */
    public function getUserBuddyPage(){
        /**
         * userId           名片编号
         */
        $this->datas = $this->params;

        $this->getUserBuddyPageService();
    }


    private function getUserBuddyPageService(){
        //按字母查询
        $sql = "SELECT z.fistPY, COUNT(1) AS num FROM (SELECT LEFT (pinyin(u.user_name), 1) AS fistPY FROM user_buddy b LEFT JOIN `user` u ON u.id = b.buddy_user_id WHERE b.user_id = ".$this->datas['userId']." AND b. STATUS = 1 ) z GROUP BY z.fistPY ORDER BY z.fistPY";
        $res = userBuddyDB::query($sql);

        //查询每个字母信息
        if(!empty($res)){
            foreach($res as &$value){
                $dataSql = "SELECT u.id AS id, b.id AS buddyId, u.user_name AS userName, u.user_img AS userImg, u.institute AS institute,ifnull(u.job_title,'') as jobTitle,ifnull(u.e_user_name,'') AS eUserName,ifnull(u.subject,'') AS subject,ifnull(u.research_direction,'') AS researchDirection,ifnull(u.user_detail,'') AS userDetail FROM user_buddy b LEFT JOIN `user` u ON u.id = b.buddy_user_id WHERE b.user_id = ".$this->datas['userId']." AND b. STATUS = 1 and left(pinyin(u.user_name),1) = '".$value['fistPY']."' ORDER BY u.user_name,u.id";
                $dataRes = userBuddyDB::query($dataSql);
                $value['data'] = $dataRes;
            }
        }


        //查询总数
        $countsql = "SELECT count(1) as num FROM user_buddy b LEFT JOIN `user` u on u.id = b.buddy_user_id WHERE b.user_id = ".$this->datas['userId']." and b.status = 1";
        $count = userBuddyDB::query($countsql);


        $this->returnMsg(1, '获取数据成功！', $res, $count[0]["num"]);
    }



    /**
     * 删除名片夹名片
     */
    public function delUserBuddyPage(){
        /**
         * buddyId           名片夹编号
         */
        $this->datas = $this->params;

        $this->delUserBuddyPageService();
    }


    private function delUserBuddyPageService(){

        $updatesql = "UPDATE user_buddy SET status = 2,update_time = NOW() where id = ".$this->datas['buddyId'];
        userBuddyDB::execute($updatesql);
        $this->returnMsg(1, '操作成功！');
    }







}